document.querySelector(".fanhui").onclick = function () {
    location.href = "./index.html"
}
document.querySelector(".seacher").onclick = function () {
    location.href = "./search.html"
}

// 数据详情渲染
async function listshow() {
    try {
        let response = await ajaxUsePromise02({
            url: "http://localhost:3000/books",
            method: "get",
        });
        console.log(response);
        ceratelist(response);
    } catch (error) {
        console.error("Error fetching data:", error);
    }
}

function ceratelist(data) {
    // 使用循环遍历数据并更新页面元素
    let listbannerElements = document.querySelectorAll('.listbanner'); // 获取所有列表项
    // console.log(listbannerElements);
    data.forEach((item, index) => {
        // console.log(index);
        let Otit = listbannerElements[index].querySelector('.listbanner-tit');
        let Oname = listbannerElements[index].querySelector('.listbanner-name');
        let Opic = listbannerElements[index].querySelector('.listbanner-pic');
        let listbannerimg = listbannerElements[index].querySelector('.listbannerimg');
        let listbannerbtn = listbannerElements[index].querySelector('.listbanner-btn');
        let imgsrc = item.img
        listbannerimg.src = imgsrc;
        Otit.innerHTML = item.name;
        Oname.innerHTML = item.author;
        Opic.innerHTML = "￥" + item.price;
        listbannerbtn.onclick = function () {
            if (getCookie("userphone") && getCookie("userpass")) {
                location.href = `./list.html?category=${item.id}`
            } else {
                undefcookie()
            }

        }
    });
}

function undefcookie() {
    layui.use(function () {
        var layer = layui.layer;
        var util = layui.util;
        // 事件
        util.on('lay-on', {
            "test-count-down": function () {
                layer.alert('还未登陆,快去登录，五秒后转至登录页面', {
                    time: 5 * 1000,
                    success: function (layero, index) {
                        var timeNum = this.time / 1000, setText = function (start) {
                            layer.title('<span class="layui-font-red">' + (start ? timeNum : --timeNum) + '</span> 秒后自动关闭', index);
                        };
                        setText(!0);
                        this.timer = setInterval(setText, 1000);
                        if (timeNum <= 0) clearInterval(this.timer);
                    },
                    end: function () {
                        location.href = `./login.html`
                        removeCookie("userphone");
                        removeCookie("userpass");
                        removeCookie("iddres");
                        removeCookie("paytime");
                        clearInterval(this.timer);
                    }
                });
            }
        })
    });
}

window.onload = function () {
    listshow();
}

